package com.gzj.environment.module.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gzj.environment.model.system.entity.Resource;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ResourceMapper extends BaseMapper<Resource> {

    /**
     * 根据资源名称查找资源信息
     * @param name 资源名称
     * @return
     */
    @Select("select id, parent_id, name, type, url, permission, icon, sort from sys_resource where name = #{name} " +
            "and deleted = 0")
    Resource findByName(@Param("name") String name);

    /**
     * 根据roleId查询该角色拥有的所有权限
     * @param roleId 角色id
     * @return
     */
    @Select("select r.id, r.parent_id, r.name, r.type, r.url, r.permission, r.icon, r.sort from sys_resource r " +
            "left join sys_role_resource rr on r.id = rr.resource_id where rr.role_id = #{roleId} and r.deleted = 0")
    List<Resource> findByRoleId(@Param("roleId") Long roleId);

    /**
     * 根据父id查询资源信息
     * @param parentId 父id
     * @return
     */
    @Select("select id, parent_id, name, type, url, permission, icon, sort from sys_resource " +
            "where parent_id = #{parentId} and deleted = 0")
    List<Resource> findByParentId(@Param("parentId") Long parentId);
}
